﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TechTalk.SpecFlow;
using System.Drawing.Imaging;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using CERT.PageLibrary.Base;

namespace CERT.AcceptanceTests.Features
{
    [Binding]
    public class IncidentFeatureSteps : TestFixtureBase
    {
        private int VictimCount { get; set; }
        private int LocationCount { get; set; }
        private int IncidentCount { get; set; }


        [Given(@"I have selected the incident page")]
        public void GivenIHaveSelectedTheIncidentPage()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            // base.WaitUpTo(500,"Selected Incident Page Wait");

            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/#/Incidents");
            //CurrentDriver.Url = "localhost:6014/#/Incidents";
            base.WaitUpTo(500, "Selected Incident Page Wait");

            //incident count
            var incidents = CurrentDriver.FindElementsById("incidentTitle");
            if ( incidents != null )
            this.IncidentCount = incidents.Count;

        }

        [When(@"I press add New Incident")]
        public void WhenIPressAddNewIncident()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            var newbutton = CurrentDriver.FindElementById("AddNewIncident");
            base.WaitUpTo(500, "Selected Incident Page Wait");
            newbutton.Click();
            base.WaitUpTo(1000, "Selected Incident Page Wait");

        }

        [When(@"I fill in the incident page")]
        public void WhenIFillInTheIncidentPage(Table table2)
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            base.WaitUpTo(500, "Selected Incident Page Wait");
            foreach (var row in table2.Rows)
            {

                var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                textField.Clear();
                textField.SendKeys(row["value"]);
                // Assert.IsTrue(textField.Text.Contains(row["value"]));

            }

            var teams = CurrentDriver.FindElementsById("team");
            base.WaitUpTo(50, "Selected Incident Page Wait");
            var team = teams.First(a => a.TagName == "input");
            team.Click();
            base.WaitUpTo(500, "Selected Incident Page Wait");

        }

        [When(@"I press save incident")]
        public void WhenIPressSaveIncident()
        {
            var textField = CurrentDriver.FindElementById("saveButton");
            textField.Click();
            base.WaitUpTo(1000, "Selected Incident Page Wait");

        }

        [Then(@"the result should be a new incident")]
        public void ThenTheResultShouldBeANewIncident()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];


            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/#/Incidents");
            base.WaitUpTo(200, "Selected Incident Page Wait");

            var incidents = CurrentDriver.FindElementsById("incidentTitle");

            Assert.Greater(incidents.Count, this.IncidentCount);
        
        }

        [When(@"I select the incident page")]
        public void WhenISelectTheIncidentPage()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
           
            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/#/Incidents");

        }


        [When(@"I select the last incident")]
        public void WhenISelectTheLastIncident()
        {

            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            base.WaitUpTo(500, "get incident page failed");
            var incidents = CurrentDriver.FindElementsById("incidentTitle");
            var incident = incidents.Last(a => a.TagName == "a");
            incident.Click();
        }

        [When(@"I edit the title in the incident page")]
        public void WhenIEditTheTitleInTheIncidentPage(Table table4)
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            this.WaitUpTo(1000, "get incident page failed");
            foreach (var row in table4.Rows)
            {

                var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                textField.Clear();
                textField.SendKeys(row["value"]);
                // Assert.IsTrue(textField.Text.Contains(row["value"]));

            }

            var savebutton = CurrentDriver.FindElementById("buttonSave");
            savebutton.Click();
            this.WaitUpTo(1000, "save incident page failed");

        }

        [Then(@"the result should be a revised incident")]
        public void ThenTheResultShouldBeARevisedIncident()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            Assert.AreEqual(CurrentDriver.Title.ToLower(), CurrentDriver.Title.ToLower());
        }


        [When(@"I press the press the delete incident button")]
        public void WhenIPressThePressTheDeleteIncidentButton()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            var deletebuttons = CurrentDriver.FindElementsById("deleteIncident");
            deletebuttons.Last(a => a.TagName == "button").Click();

        }

        [Then(@"the result should be one less incident")]
        public void ThenTheResultShouldBeOneLessIncident()
        {
            var deletebuttons = CurrentDriver.FindElementsById("deleteIncident");
            Assert.Less(deletebuttons.Count, IncidentCount);
        }

        [When(@"I create a new location")]
        public void WhenICreateANewLocation(Table table6)
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            this.WaitUpTo(1000, "get new location page failed");
  
            //location count
            var loclist = CurrentDriver.FindElementsById("locationDetial");
            if (loclist != null )
            this.LocationCount = loclist.Count;

            var newbutton = CurrentDriver.FindElementById("newLocation");
            newbutton.Click();

            this.WaitUpTo(1000, "get new location page failed");
            var loctypes = CurrentDriver.FindElementsByName("locationTypeId");
            var loc = loctypes.First(a => a.TagName == "input");
            loc.Click();

            var conditions = CurrentDriver.FindElementsByName("locationStatus_LookupID");
            var condition = conditions.First(a => a.TagName == "input");
            condition.Click();

            foreach (var row in table6.Rows)
            {
                var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                textField.Clear();
                textField.SendKeys(row["value"]);
            }

            var hazardtypes = CurrentDriver.FindElementsByName("hazard");
            var hazard = hazardtypes.First(a => a.TagName == "input");
            hazard.Click();

            var savebutto = CurrentDriver.FindElementById("buttonSave");
            savebutto.Click();
            base.WaitUpTo(1000, "get incident page failed");

            var victim = CurrentDriver.FindElementById("victimsbutton");
            victim.Click();
            base.WaitUpTo(1000, "get incident page failed");
        }

        [When(@"I add a victim")]
        public void WhenIAddAVictim()
        {
            var victim = CurrentDriver.FindElementById("addVictim");
            victim.Click();
            base.WaitUpTo(100, "get incident page failed");
            var ages = CurrentDriver.FindElementById("Age");
            var opts = ages.FindElements(By.TagName("option"));
            opts.Last().Click();

            // ages.Last(a => a.TagName == "option").Click();


            var status = CurrentDriver.FindElementById("Status");
            opts = status.FindElements(By.TagName("option"));
            var opt = opts.Last();
            opt.Click();

            var gender = CurrentDriver.FindElementById("Gender");
            opts = gender.FindElements(By.TagName("option"));
            opt = opts.Last();
            opt.Click();

            var victimSave = CurrentDriver.FindElementById("saveButtonAdd");
            victimSave.Click();
            base.WaitUpTo(100, "get incident page failed");


        }

        [Then(@"the result is a completed incident")]
        public void ThenTheResultIsACompletedIncident()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/#/Incidents");
            WaitUpTo(500, "Incident Load Failed.");
            var incidents = CurrentDriver.FindElementsById("incidentTitle");
            var incident = incidents.Last(a => a.TagName == "a");
            incident.Click();

            this.WaitUpTo(500, "get new location page failed");
  
            //location count
            var loclist = CurrentDriver.FindElementsById("locationDetial");
            if (loclist != null)
                Assert.Greater(loclist.Count, this.LocationCount);

        }


        [When(@"I edit a location")]
        public void WhenIEditALocation()
        {
            //select the 1st location
            this.WaitUpTo(2000, "Location Details failed to load");
            var loclist = CurrentDriver.FindElementsById("locationDetial");
            this.WaitUpTo(500, "Location Details failed to load");
            var loc = loclist.First(a => a.TagName == "a");
            base.WaitUpTo(250, "get incident page failed");
            loc.Click();

            this.WaitUpTo(500, "Location Details failed to load");

            var hazardtypes = CurrentDriver.FindElementsByName("hazard");
            var hazard = hazardtypes.Skip(1).First(a => a.TagName == "input");
            hazard.Click();

            var savebutto = CurrentDriver.FindElementById("buttonSave");
            
            savebutto.Click();
            base.WaitUpTo(1000, "get incident page failed");
        }

        [When(@"I add a new victim")]
        public void WhenIAddANewVictim()
        {
            //victim count

            var victim = CurrentDriver.FindElementById("victimsbutton");

            this.WaitUpTo(1000, "get new location page failed");

            victim.Click();
            base.WaitUpTo(1000, "get incident page failed");

                   //location count
            var viclist = CurrentDriver.FindElementsById("victimrow");
            if (viclist != null)
                this.VictimCount = viclist.Count;

            var victima = CurrentDriver.FindElementById("addVictim");
            victima.Click();
            base.WaitUpTo(100, "get incident page failed");

            var ages = CurrentDriver.FindElementById("Age");
            var opts = ages.FindElements(By.TagName("option"));
            var opt = opts.Last();
            opt.Click();


            var status = CurrentDriver.FindElementById("Status");
            opts = status.FindElements(By.TagName("option"));
             opt = opts.Last();
            opt.Click();

            var gender = CurrentDriver.FindElementById("Gender");
            opts = gender.FindElements(By.TagName("option"));
            opt = opts.Last();
            opt.Click();


            var victimSave = CurrentDriver.FindElementById("saveButtonAdd");
            victimSave.Click();
            base.WaitUpTo(100, "get incident page failed");


        }

        [Then(@"the result is a revised incident")]
        public void ThenTheResultIsARevisedIncident()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];


            //location count
            var viclist = CurrentDriver.FindElementsById("victimrow");
            if (viclist != null)
                Assert.Greater(viclist.Count, this.VictimCount); // = viclist.Count;
        }

    }
}
